//
//  SPDocUploadModel.m
//  
//
//  Created by SPSuper on 2017/7/12.
//  Copyright © 2017年 etonedu. All rights reserved.
//

#import "WLFileUpDownLoadModel.h"
#import "WLFileHandle.h"
#import "NSString+Ext.h"
#import "WLFileRequest.h"
#define kSuperUploadBlockSize 1* 1024 * 1024

@implementation WLFileUpDownLoadModel

+ (NSArray *)bg_ignoreKeys {
    return @[@"dataTask"];
}
- (NSString *)fileTotalSize {
    if (_fileTotalSize == nil) {
        _fileTotalSize = [NSString formateSizeWithBytes:self.fileTotalBytes];
    }
    
    return _fileTotalSize;
}
//- (unsigned long long)fileTotalBytes {
//    if (_fileTotalBytes == 0) {
//        if ([self.fileData length] > 0) {
//            _fileTotalBytes = self.fileData.length;
//        } else if (self.filePath) {
//            _fileTotalBytes = [[WLFileHandle sizeOfItemAtPath:self.filePath error:nil] unsignedLongLongValue];
//        }
//    }
//    return _fileTotalBytes;
//}
- (void)setFileName:(NSString *)fileName {
    if (fileName.length > 0) {
        NSString *extentsion = fileName.pathExtension;
        _fileName = [fileName stringByReplacingOccurrencesOfString:extentsion withString:extentsion.lowercaseString];
    }
}
- (NSURL *)fileURL {
    if (_fileURL == nil) {
        NSString *filePath = nil;
        NSString *documentPath = [WLFileHandle documentsDir];
        if (self.fileType == SPFileTypePhoto) {
            filePath = [documentPath stringByAppendingPathComponent:@"photo"];
        } else if (self.fileType == SPFileTypeVideo) {
            filePath = [documentPath stringByAppendingPathComponent:@"video"];
        } else if (self.fileType == SPFileTypeMusic) {
            filePath = [documentPath stringByAppendingPathComponent:@"music"];
        }
        filePath =  [filePath stringByAppendingPathComponent:self.fileName];
        _fileURL = [NSURL fileURLWithPath:filePath];
    } else {//由于每次重启都会导致路径有问题，这里需要在获取的时候重新刷新路径
        NSString *documentPath = [WLFileHandle documentsDir];
        if (![_fileURL.path containsString:documentPath]) {
            _fileURL = [NSString createRightDocumentFileURL:_fileURL];
        }
    }
    return _fileURL;
}
- (WLFileRequest *)getRequestModel {
    WLFileRequest *reqeust = [[WLFileRequest alloc]init];
    reqeust.fileName = self.fileName;
    int fileType = 0;
    switch (self.fileType) {
        case SPFileTypeVideo:
            fileType = 0;
            break;
        case SPFileTypePhoto:
            fileType = 1;
            break;
        case SPFileTypeMusic:
            fileType = 2;
            break;
    }
    reqeust.fileType = fileType;
    reqeust.fileURL = self.fileURL;
    reqeust.mediaLocalURL = self.mediaLocalURL;
    reqeust.fileTotalBytes = (long)self.fileTotalBytes;
    reqeust.reqeustURL = self.requestURL;
    reqeust.timeOut = 1000;
    WLFileRequestConfig *reqeustConfig = [[WLFileRequestConfig alloc] init];
    reqeustConfig.saveToAbulm = self.saveToAbulm;
    reqeust.requestConfig = reqeustConfig;
    return reqeust;
}

- (WLFileRequest *)getRequestModelNotSave {
    WLFileRequest *reqeust = [self getRequestModel];
    reqeust.requestConfig.saveToAbulm = NO;
    return reqeust;
}

+ (NSString *)getTableNameWithModelType:(WLDataBaseModelType)modelType {
    switch (modelType) {
        case WLDataBaseModelTypeUpload:
            return @"WLDataBaseModelTypeUpload";
            break;
        case WLDataBaseModelTypeUploaded:
            return @"WLDataBaseModelTypeUploaded";
            break;
        case WLDataBaseModelTypeDownload:
            return @"WLDataBaseModelTypeDownload";
            break;
        case WLDataBaseModelTypeDownloaded:
            return @"WLDataBaseModelTypeDownloaded";
            break;
        default:
            return nil;
            break;
    }
}
@end

